package org.ling.xuan.pannel;

import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBPanel;
import com.intellij.ui.components.JBPasswordField;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.components.JBTextArea;
import com.intellij.ui.components.JBTextField;
import org.ling.xuan.entity.mo.CommonSetting;

import javax.swing.border.LineBorder;
import java.awt.*;

public class CommonSettingPanel extends JBPanel {
    private CommonSetting commonSetting;

    private final JBTextField userName;
    private final JBPasswordField password;
    private final JBTextField auth;
    private final JBTextField emailContact;

    private final JBTextField dbUrl;
    private final JBTextField dbUser;
    private final JBPasswordField dbPassword;
    private final JBTextField dbType;
    private final JBTextField dbDriver;
    
    public CommonSettingPanel(CommonSetting commonSetting) {
        this.commonSetting = commonSetting;
        if (this.commonSetting == null) {
            this.commonSetting = new CommonSetting();
        }
        userName = new JBTextField();
        password = new JBPasswordField();
        auth = new JBTextField();
        emailContact = new JBTextField();
    
        dbUrl = new JBTextField(200);
        dbUser = new JBTextField(50);
        dbPassword = new JBPasswordField();
        dbType = new JBTextField(50);
        dbDriver = new JBTextField(50);
        setLayout(new GridLayout(9, 2));
        customAdd(userName, "用户名:", commonSetting.getUserName());
        customAdd(password, "密码:", commonSetting.getPassword());
        customAdd(auth, "开发昵称(英文):", commonSetting.getAuth());
        customAdd(emailContact, "邮箱联系人:", commonSetting.getEmailContact());
        customAdd(dbUrl, "开发数据库url:", commonSetting.getDbUrl());
        customAdd(dbUser, "开发数据库用户:", commonSetting.getDbUser());
        customAdd(dbPassword, "开发数据库密码:", commonSetting.getDbPassword());
        customAdd(dbType, "开发数据库类型:", commonSetting.getDbType());
        customAdd(dbDriver, "开发数据库驱动:", commonSetting.getDbDriver());
    }

    public void customAdd(JBTextField field, String label, String text) {
        add(new JBLabel(label));
        if (text != null) {
            field.setText(text);
        } else {
            field.setText("");
        }
        add(field);

    }

    public void customAdd(JBTextArea field, String label, String text) {
        add(new JBLabel(label));
        field.setBorder(new LineBorder(Color.RED));
        field.setMargin(new Insets(5, 5, 5, 5));
        field.setLineWrap(true); // Enable word wrap
        field.setWrapStyleWord(true);
        if (text != null) {
            field.setText(text);
        } else {
            field.setText("");
        }
        JBScrollPane scrollPane = new JBScrollPane(field);
        add(scrollPane);

    }

    public void customAdd(JBPasswordField field, String label, String text) {
        add(new JBLabel(label));
        if (text != null) {
            field.setText(text);
        } else {
            field.setText("");
        }
        add(field);
    }

    public CommonSetting getCommonSetting() {
        commonSetting.setUserName(userName.getText());
        commonSetting.setPassword(String.valueOf(password.getPassword()));
        commonSetting.setAuth(auth.getText());
        commonSetting.setEmailContact(emailContact.getText());
        commonSetting.setDbUrl(dbUrl.getText());
        commonSetting.setDbUser(dbUser.getText());
        commonSetting.setDbPassword(String.valueOf(dbPassword.getPassword()));
        commonSetting.setDbType(dbType.getText());
        commonSetting.setDbDriver(dbDriver.getText());
        return commonSetting;
    }
}
